package cn.hutool.benchmark;

import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.core.util.RandomUtil;
import org.openjdk.jmh.annotations.*;

import java.util.concurrent.TimeUnit;

@BenchmarkMode(Mode.AverageTime)//每次执行平均花费时间
@Warmup(iterations = 5, time = 1) //预热5次调用
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) // 执行5此，每次1秒
@Threads(1) //单线程
@Fork(1) //
@OutputTimeUnit(TimeUnit.NANOSECONDS) // 单位：纳秒
@State(Scope.Benchmark) // 共享域
public class concatJmh {

	private String[] strs;

	@Setup
	public void setup(){
		int length = 10;
		strs = new String[length];
		for(int i = 0; i < length; i++){
			strs[i] = RandomUtil.randomString(length * length);
		}
	}

	@Benchmark
	public String strPlusTest(){
		String result = strs[0];
		for(int i = 1; i < strs.length;i++) {
			result += strs[i];
		}
		return result;
	}

	@Benchmark
	public String strConcatTest(){
		String result = strs[0];
		for(int i = 1; i < strs.length;i++) {
			result = result.concat(strs[i]);
		}
		return result;
	}

	@Benchmark
	public String hutoolConcatTest(){
		return StrUtil.concat(false, this.strs);
	}
}
